<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="heroId">
      <el-input v-model="dataForm.heroId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="userId">
      <el-input v-model="dataForm.userId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="lv">
      <el-input v-model="dataForm.lv" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="exp">
      <el-input v-model="dataForm.exp" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="att">
      <el-input v-model="dataForm.att" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="def">
      <el-input v-model="dataForm.def" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="spDef">
      <el-input v-model="dataForm.spDef" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="life">
      <el-input v-model="dataForm.life" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="speed">
      <el-input v-model="dataForm.speed" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="attGrow">
      <el-input v-model="dataForm.attGrow" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="defGrow">
      <el-input v-model="dataForm.defGrow" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="spDefGrow">
      <el-input v-model="dataForm.spDefGrow" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="lifeGrow">
      <el-input v-model="dataForm.lifeGrow" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="status">
      <el-input v-model="dataForm.status" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="isDel">
      <el-input v-model="dataForm.isDel" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          heroId: '',
          userId: '',
          lv: '',
          exp: '',
          att: '',
          def: '',
          spDef: '',
          life: '',
          speed: '',
          attGrow: '',
          defGrow: '',
          spDefGrow: '',
          lifeGrow: '',
          status: '',
          isDel: ''
        },
        dataRule: {
          heroId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          lv: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          exp: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          att: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          def: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          spDef: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          life: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          speed: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          attGrow: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          defGrow: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          spDefGrow: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          lifeGrow: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isDel: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/uhero/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.heroId = data.uhero.heroId
                this.dataForm.userId = data.uhero.userId
                this.dataForm.lv = data.uhero.lv
                this.dataForm.exp = data.uhero.exp
                this.dataForm.att = data.uhero.att
                this.dataForm.def = data.uhero.def
                this.dataForm.spDef = data.uhero.spDef
                this.dataForm.life = data.uhero.life
                this.dataForm.speed = data.uhero.speed
                this.dataForm.attGrow = data.uhero.attGrow
                this.dataForm.defGrow = data.uhero.defGrow
                this.dataForm.spDefGrow = data.uhero.spDefGrow
                this.dataForm.lifeGrow = data.uhero.lifeGrow
                this.dataForm.status = data.uhero.status
                this.dataForm.isDel = data.uhero.isDel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/uhero/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'heroId': this.dataForm.heroId,
                'userId': this.dataForm.userId,
                'lv': this.dataForm.lv,
                'exp': this.dataForm.exp,
                'att': this.dataForm.att,
                'def': this.dataForm.def,
                'spDef': this.dataForm.spDef,
                'life': this.dataForm.life,
                'speed': this.dataForm.speed,
                'attGrow': this.dataForm.attGrow,
                'defGrow': this.dataForm.defGrow,
                'spDefGrow': this.dataForm.spDefGrow,
                'lifeGrow': this.dataForm.lifeGrow,
                'status': this.dataForm.status,
                'isDel': this.dataForm.isDel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
